package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcMemberLevel;
import com.csun.cmny.provider.model.dto.memberLevel.MemberLevelQueryDto;
import com.csun.cmny.provider.model.dto.memberLevel.MemberLevelSaveDto;
import com.csun.cmny.provider.model.dto.memberLevel.MemberLevelUpdateDto;
import com.csun.cmny.provider.model.vo.memberLevel.MemberLevelVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcMemberLevelService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/memberLevel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMemberLevelController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcMemberLevelService omcMemberLevelService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid MemberLevelSaveDto memberLevelSaveDto, BindingResult br) {

        //校验这个等级名称是否被使用过
        OmcMemberLevel level = new OmcMemberLevel();
        level.setLevelName(memberLevelSaveDto.getLevelName());
        List<OmcMemberLevel> select = omcMemberLevelService.select(level);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("memberLevel.repeat.null", null));
        }

        //校验这个等级标准是否被使用过
        OmcMemberLevel memberLevel = new OmcMemberLevel();
        memberLevel.setLevelStandard(memberLevelSaveDto.getLevelStandard());
        List<OmcMemberLevel> list = omcMemberLevelService.select(memberLevel);
        if(PublicUtil.isNotEmpty(list)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("memberLevel.levelStandard.repeat", null));
        }

        OmcMemberLevel omcMemberLevel = new OmcMemberLevel();
        BeanUtils.copyProperties(memberLevelSaveDto,omcMemberLevel);

        omcMemberLevel.setCreator(getLoginAuthDto().getUserName());
        omcMemberLevel.setCreatorId(getLoginAuthDto().getUserId());
        omcMemberLevel.setGroupId(getLoginAuthDto().getGroupId());
        int relust = omcMemberLevelService.save(omcMemberLevel);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @RequestMapping(value = "/listPage", method = RequestMethod.POST)
    public Wrapper listPage(@RequestBody @Valid MemberLevelQueryDto memberLevelQueryDto, BindingResult br) {


        List<MemberLevelVo>list=omcMemberLevelService.selectPageQuery(memberLevelQueryDto);

        PageInfo<MemberLevelVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody @Valid MemberLevelUpdateDto memberLevelUpdateDto, BindingResult br) {

        if(PublicUtil.isNotEmpty(memberLevelUpdateDto.getLevelName())){
            OmcMemberLevel level = new OmcMemberLevel();
            level.setLevelName(memberLevelUpdateDto.getLevelName());
            List<OmcMemberLevel> select = omcMemberLevelService.select(level);
            if(PublicUtil.isNotEmpty(select)&&!select.get(0).getId().equals(memberLevelUpdateDto.getId())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("memberLevel.repeat.null", null));
            }
        }

        //校验这个等级标准是否被使用过
        if(PublicUtil.isNotEmpty(memberLevelUpdateDto.getLevelStandard())){
            OmcMemberLevel memberLevel = new OmcMemberLevel();
            memberLevel.setLevelStandard(memberLevelUpdateDto.getLevelStandard());
            List<OmcMemberLevel> list = omcMemberLevelService.select(memberLevel);
            if(PublicUtil.isNotEmpty(list)&&!list.get(0).getId().equals(memberLevelUpdateDto.getId())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("memberLevel.levelStandard.repeat", null));
            }
        }

        OmcMemberLevel omcMemberLevel = new OmcMemberLevel();
        BeanUtils.copyProperties(memberLevelUpdateDto,omcMemberLevel);
        int relust = omcMemberLevelService.update(omcMemberLevel);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }
}
